$(function () {
    $('#myTable').dataTableExtend({
        "ajax": {
            "url": sysInfo.basePath + "/zs/query_certificate/query_certificate.do",
            "type": "post"
        }, "fnServerParams": function (aoData) {
            var checkResult = checkHasSybAuth($('#signOrgId option'));
            if (!checkResult) {
                if ($("#permissions").val()) {
                    aoData = $.extend(aoData, {"isAll": "0"});
                } else {
                    aoData = $.extend(aoData, {"isAll": "1"});
                }
            }
            aoData = $.extend(aoData, $('.titleSearch').serialize2object());
            return aoData;
        },
        "pageLength": 20,//每页显示的条数默认是10条，有特殊业务需要显示不同条数的列表才应该加入该参数
        "scrollX": true,//是否开启横项滚动
        "fixedColumns": {//锁定列，只有开启scrollX时才生效
            "leftColumns": 3,//左侧锁定2列
            "rightColumns": 2//右侧锁定1列
        },
        "aoColumns": [
            {
                "sClass": "check-th",
                "title": "<input type=\"checkbox\" class=\"checkall\" value=\"undefined\">",
                "orderable": false,
                "data": "baseId",
                "render": function (data, type, full, meta) {
                    return '<input type="checkbox"  class="checkchild"  value="' + data + '" />';
                }
            },
            {
                "title": "所属事业部",
                "data": "signOrgName",
                "orderable": false
            },
            {
                "title": "名称/编号",
                "data": "certificateNo",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    return full.certificateName + "<br/><a href='javascript:void(0);'  class='layui-table-link' onclick='credentialDetail(\"" + data + "\",\"" + full.baseId + "\")'>" + data + "</a>";
                }
            },

            {
                "title": "持证人/身份证号码",
                "data": "personalIdCard",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return row.personalName + "<br/><span>" + hideIdCard(data) + "</span>";
                }
            },
            {
                "title": "类型/专业/等级",
                "data": "certificateMajor",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    data = (null != data && "" != data) ? data : "-";
                    return row.certificateType + "<br/><div title='" + data + "' class='f-toe' style=\"max-width:100px;word-wrap:break-word\">" + data + "</div>" + row.certificateLevel;
                }
            },
            {
                "title": "类别",
                "data": "certificateCategory",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    data = (null != data && "" != data) ? data : "-";
                    return data;
                }
            },
            {
                "title": "地区",
                "data": "certificateArea",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    areaProvince = (null != row.areaProvince && "" != row.areaProvince) ? row.areaProvince : "";
                    area = (null != row.certificateArea && "" != row.certificateArea) ? "/" + row.certificateArea : "";
                    return areaProvince + area;
                }
            },
            {
                "title": "最新入库时间",
                "data": "lastWarehousingTime",
                "orderable": false
            },
            {
                "title": "操作人",
                "data": "outgoingOperaterName",
                "orderable": false
            },
            {
                "title": "领用人",
                "data": "certificateUserName",
                "orderable": false
            },
            {
                "title": "出库时间",
                "data": "lastOutgoingTime",
                "orderable": false
            },
            {
                "title": "出库原因",
                "data": "outgoingReason",
                "orderable": false
            },
            {
                "title": "备注",
                "data": "remarks",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    data = (null != data && "" != data) ? data : "-";
                    return data;
                }
            }
        ],
    });

    // 加载事业部
    syb('[name="signOrgId"]');
    loadCertificateType('190px', '#certificateType', $("#generalType").val(), 0);

    $(document).ready(function () {
        areaLength($('#remark'), 500)
    });

    chooseUserPlug.init($('.chooseUser'), {
        width: '300px',
        url: sysInfo.basePath + '/zzsc/common/find_user_choose.do',
        // multiple:true,// 单选false或不写该参数，多选true
        otherParam: {
            /**
             * type: 用户信息选择类型（必填）
             *       all:全部用户，division：当前登录人所在事业部，department:当前登录人所在部门及以下，role: 查询指定角色用户，
             * roleCode : 角色Code，（type为role时，必填）
             * locked: 用户状态，（选填，默认全部状态）
             *      0、正常；1、锁定；2、离职
             */
            type: 'division',
            locked: '0'
        }
    });
});

/**
 * 导出
 * @returns {boolean}
 */
function exportCertificate() {
    var params = $("#titleSearch").serializeObject();
    var isNotNull = 0;
    $.each(params, function (i, item) {
        if (!checkIsEmpty(item)) {
            isNotNull = isNotNull + 1;
            return false;
        }
    });
    if (isNotNull === 0) {
        DGG.Errors("请选择搜索条件后进行导出!");
        return false;
    }
    var checkResult = checkHasSybAuth($('#signOrgId option'));
    if (!checkResult) {
        if ($("#permissions").val()) {
            params["isAll"] = "0";
        } else {
            params["isAll"] = "1";
        }
    }
    export_table("/zs/query_certificate/export_qyery_certificate.do", params);
}

/**
 * 证书转交
 */
function transfer() {
    var data = $('#myTable').dataTableExtend('getCheck');
    if (data.length == 0) {
        DGG.Warning('请选择证书!');
        return false;
    }
    if (data.length > 1) {
        DGG.Warning('只能选择一个证书!');
        return false;
    }
    var index = dgg.Open({
        title: '转交',
        content: $('#transfer'),
        area: '500px',
        yes: function () {
            var referredPeople = $("#referredPeople").select2('val');
            var remark = $("#remark").val();
            if (!referredPeople) {
                DGG.Warning('请选择转交人！');
                return false;
            }
            var baseId = data[0].baseId;
            DGG.loading();
            $.post(sysInfo.basePath + "/zs/query_certificate/transfer_certificate.do", {
                "baseId": baseId,
                "referredPeople": referredPeople,
                "remark": remark
            }, function (result) {
                DGG.loading('close');
                if (result.code == 0) {
                    layer.close(index);
                    DGG.Succeed('转交成功！');
                    $("#remark").val("");
                    $('#myTable').dataTableExtend("reload");
                } else {
                    DGG.Errors(result.msg);
                }
            });

        }
    });
}

